/* eslint-disable no-param-reassign */
import { hideLoading, showLoading, showToast } from '@ray-js/ray';
import { createAsyncThunk, createSelector, createSlice, PayloadAction } from '@reduxjs/toolkit';
import { ReduxState } from '..';
import { devices } from '@/devices';
import Strings from '@/i18n';
import { saveCloudData } from '@/api/index';
import { CLOUD_DATA_KEYS_MAP } from '@/constant';

export interface ColourCustom {
  hue: number;
  saturation: number;
  value: number;
}
export interface WhiteCustom {
  brightness: number;
  temperature: number;
}

type CloudState = {
  switchName: Record<string, string>;
  switchPic: Record<string, string>;
  groupNames: Record<string, string>;
  delayTime: number;
};

/**
 * Slice
 */
const cloudStateSlice = createSlice({
  name: 'cloudState',
  initialState: {
    switchName: {
      switch_1: '',
      switch_2: '',
      switch_3: '',
      switch_4: '',
      switch_5: '',
      switch_6: '',
      switch_7: '',
      switch_8: '',
      switch_9: '',
      switch_10: '',
    },
    switchPic: {
      switch_1: '',
      switch_2: '',
      switch_3: '',
      switch_4: '',
      switch_5: '',
      switch_6: '',
      switch_7: '',
      usb_switch_1: '',
      usb_switch_2: '',
      switch9: '',
      switch10: '',
    },

    groupNames: {
      group_1: '',
      group_2: '',
      group_3: '',
      group_4: '',
      group_5: '',
    },
    delayTime: new Date().getTime(),
  } as CloudState,
  reducers: {
    initCloud(_, action: PayloadAction<CloudState>) {
      return action.payload;
    },
    updateSwtichName(state, action: PayloadAction<CloudState['switchName']>) {
      state.switchName = { ...state.switchName, ...action.payload };
    },
    updateSwtichPic(state, action: PayloadAction<CloudState['switchPic']>) {
      state.switchPic = { ...state.switchPic, ...action.payload };
    },
    updateGroupNames(state, action: PayloadAction<CloudState['groupNames']>) {
      state.groupNames = { ...state.groupNames, ...action.payload };
    },
    updatetDelayTime(state, action: PayloadAction<number>) {
      state.delayTime = action.payload;
    },
  },
});
/**
 * Actions
 */
export const { initCloud, updateSwtichName, updateSwtichPic, updateGroupNames, updatetDelayTime } =
  cloudStateSlice.actions;

/**
 * Selectors
 */

/**
 * Effects
 */

// export const saveCloudDataAsync = createAsyncThunk(
//   'cloudState/saveCloudDataAsync',
//   async ({ code, value }, thunkApi) => {

//     return Promise.all([saveCloudData({ code, value })])
//       .then(data => {
//         thunkApi.dispatch(updateSwtichName({ [code]: value }));
//       })
//       .catch(err => {
//         // hideLoading();
//         showToast({ title: '获取云端数据失败' });
//         console.log('=== storage.get failed', err);
//       });
//   }
// );

export default cloudStateSlice.reducer;
